package com.t.bricks.bussiness.db.dao.loginpast.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.t.bricks.bussiness.db.dao.loginpast.LoginPastDao;
import com.t.bricks.bussiness.db.entity.loginpast.LoginPast;
import com.t.bricks.bussiness.db.mapper.loginpast.LoginPastMapper;
import com.t.bricks.bussiness.model.loginpast.ConditionLoginPast;
import com.t.bricks.model.Log;
import com.t.bricks.model.MsgEmity;
import com.t.bricks.model.OrderInfoList;
import com.t.bricks.utils.function.data.IntegerUtil;

@Repository
public class LoginPastDaoImpl implements LoginPastDao {
	
	@Autowired
	private LoginPastMapper loginPastMapper;

	@Value("${app.findPage.dLikeDate:配置文件中的当前库全文检索时间范围dLikeDate未设置}")
	private String sLikeDate;

	/**
	 * 查询搜索用的数据
	 * 警告:本方法只允许内部使用,不能开放到外部接口
	 * @param whereStr 查询条件字符串
	 * @param orders 排序数据集合
	 * @return
	 */
	@Override
	public MsgEmity findBySearch(String whereStr, OrderInfoList orders) {
		if (null == whereStr || "".equals(whereStr.trim())) {
			return new MsgEmity(false, "查询条件不允许为空！", 7001);
		}

		List<LoginPast> list = null;
		try {
			list = loginPastMapper.findBySearch(whereStr.trim(),
					(null == orders) ? null : orders.getOrderInfoList());
			if (null == list) {
				Log.error("查询数据发生异常,执行的返回值为:null,预期值不应为null");
				return new MsgEmity(false, "查询操作失败！", 7002);
			}
		} catch (Exception e) {
			Log.error("查询分页数据发生异常:", e.getMessage());
			return new MsgEmity(false, "查询请求失败，请查看异常日志！", 7003);
		}
		
		return new MsgEmity(true, "查询成功！", list);
	}

	/**
	 * 查询导出到文件的数据
	 * @param orders 排序数据集合
	 * @param sLikeStr 全文检索
	 * @param condition 查询条件对象
	 * @return
	 */
	@Override
	public MsgEmity findByExport(OrderInfoList orders, String sLikeStr, ConditionLoginPast condition) {
		if ("配置文件中的当前库全文检索时间范围dLikeDate未设置".equals(sLikeDate)) {
			sLikeDate = "3";
		}
		
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DATE, - IntegerUtil.strToInt(sLikeDate, 3));//日期加m天
		Date dLikeDateSt = calendar.getTime();
		Date dLikeDateEd = new Date();
		
		List<LoginPast> list = null;
		try {
			list = loginPastMapper.findByExport(
					(null == orders) ? null : orders.getOrderInfoList(),
					sLikeStr, dLikeDateSt, dLikeDateEd, condition);
			if (null == list) {
				Log.error("查询数据发生异常,执行的返回值为:null,预期值不应为null");
				return new MsgEmity(false, "查询操作失败！", 7001);
			}
		} catch (Exception e) {
			Log.error("查询导出文件用的数据发生异常:", e.getMessage());
			return new MsgEmity(false, "查询请求失败，请查看异常日志！", 7002);
		}
		
		return new MsgEmity(true, "查询成功！", list);
	}

	//---------------------------- 自动生成代码结束 ----------------------------//

	/**
	 * 取指定时间内签到情况(超级管理员会被排除)
	 * 注意:sName为空则查询所有人(超级管理员还是会被排除)
	 * @param sName 用户名
	 * @param dDateSt 开始时间范围
	 * @param dDateEd 结束时间范围
	 * @return
	 */
	@Override
	public MsgEmity findByDay(String sName, Date dDateSt, Date dDateEd) {
		List<LoginPast> list = null;
		try {
			list = loginPastMapper.findByDay(sName, dDateSt, dDateEd);
			if (null == list) {
				Log.error("查询数据发生异常,执行的返回值为:null,预期值不应为null");
				return new MsgEmity(false, "查询操作失败！", 7002);
			}
		} catch (Exception e) {
			Log.error("查询分页数据发生异常:", e.getMessage());
			return new MsgEmity(false, "查询请求失败，请查看异常日志！", 7003);
		}
		
		return new MsgEmity(true, "查询成功！", list);
	}
	
	/**
	 * 简约方式添加签到日志
	 * @param sNameOrNo 登录者使用的用户名或工号
	 * @return
	 */
	@Override
	public MsgEmity add(String sNameOrNo) {
		try {
			int i = loginPastMapper.addLog(sNameOrNo);
			if (i < 1) {
				Log.error("新增未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "新增数据失败！", 7001);
			}
		} catch (Exception e) {
			Log.error("新增时发生异常:", e.getMessage());
			return new MsgEmity(false, "新增失败！", 7002);
		}
		
		return new MsgEmity(true, "新增成功！", 7999);
	}
	
	/**
	 * 删除
	 * @param dDateSt
	 * @param dDateEd
	 * @return
	 */
	@Override
	public MsgEmity del(Date dDateSt, Date dDateEd) {
		try {
			int i = loginPastMapper.del(dDateSt, dDateEd);
			if (i < 1) {
				Log.error("删除未能得到预期影响值(预期大于0):", i);
				return new MsgEmity(false, "删除数据失败！", 7002);
			}
		} catch (Exception e) {
			Log.error("删除时发生异常:", e.getMessage());
			return new MsgEmity(false, "删除失败！", 7003);
		}
		
		return new MsgEmity(true, "删除成功！", 7999);
	}
	
}
